﻿Public Class masunit
    Private UNITID = Nothing
    Private UNITNAME_EN = Nothing
    Private UNITNAME_TH = Nothing
    Private F_RX = Nothing
    Private F_LAB = Nothing
    Private F_XRY = Nothing
    Private F_TIME = Nothing
    Private F_STK = Nothing
    Private STATUS = Nothing
    Private selectMasunit = "SELECT `unitid` AS 'รหัสหน่วยนับ', `unitname_en` AS 'ชื่อหน่วยนับ(อังกฤษ)', `unitname_th` AS 'ชื่อหน่วยนับ(ไทย)', `f_rx` AS 'หน่วยนับยาและเวชภัณฑ์', `f_lab` AS 'หน่วยนับlab', `f_xry` AS 'หน่วยนับX-Ray', `f_time` AS 'หน่วยนับเวลา', `f_stk` AS 'หน่วยนับคลัง', `status` AS 'สถานะการเปิดใช้งาน' FROM masunit"
    'Private selectMasgrpdrg = "SELECT GDCODE, GRPDRG, convert(bit, [STATUS]) FROM MASGRPDRG"

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Property UNITID_ As Integer
        Get
            Return UNITID
        End Get
        Set(value As Integer)
            UNITID = value
        End Set
    End Property

    Property UNITNAME_EN_ As String
        Get
            Return UNITNAME_EN
        End Get
        Set(value As String)
            UNITNAME_EN = value
        End Set
    End Property

    Property UNITNAME_TH_ As String
        Get
            Return UNITNAME_TH
        End Get
        Set(value As String)
            UNITNAME_TH = value
        End Set
    End Property

    Property F_RX_ As Boolean
        Get
            Return F_RX
        End Get
        Set(value As Boolean)
            If value = True Then
                F_RX = 1
            Else
                F_RX = 0
            End If
        End Set
    End Property

    Property F_LAB_ As Boolean
        Get
            Return F_LAB
        End Get
        Set(value As Boolean)
            If value = True Then
                F_LAB = 1
            Else
                F_LAB = 0
            End If
        End Set
    End Property

    Property F_XRY_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                F_XRY = 1
            Else
                F_XRY = 0
            End If
        End Set
    End Property

    Property F_TIME_ As Boolean
        Get
            Return F_TIME
        End Get
        Set(value As Boolean)
            If value = True Then
                F_TIME = 1
            Else
                F_TIME = 0
            End If
        End Set
    End Property

    Property F_STK_ As Boolean
        Get
            Return F_STK
        End Get
        Set(value As Boolean)
            If value = True Then
                F_STK = 1
            Else
                F_STK = 0
            End If
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Property selectMasunit_ As String
        Get
            Return selectMasunit
        End Get
        Set(value As String)

        End Set
    End Property

    Public Function checkDupdata(ByVal UNITNAME_EN As String)
        'ตรวจสอบข้อมูลชื่อภาษาอังกฤษว่าซ้ำหรือไม่ ถ้าซ้ำจะทำการปิดปุ่มเพิ่มไม่ให้เพิ่มข้อมูลนั้น
        If connect.GetTable("SELECT unitid FROM masunit WHERE unitname_en = '" & UNITNAME_EN & "'").Rows.Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub insertMasunitData()
        If connect.GetTable("SELECT unitid FROM masunit WHERE unitname_en = '" & UNITNAME_EN & "' OR unitname_th = '" & UNITNAME_TH & "'").Rows.Count > 0 Then
            MsgBox("มีข้อมูลหน่วยนับนี้อยู่ในระบบแล้ว")
        Else
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()
                'Dim dischargeID As Integer

                connect.ExecuteNonQuery("INSERT INTO masunit (unitname_en, unitname_th, f_rx, f_lab, f_xry, f_time, f_stk, status) VALUES ('" & UNITNAME_EN & "', '" & UNITNAME_TH & "', '" & F_RX & "', '" & F_LAB & "', '" & F_XRY & "', '" & F_TIME & "', '" & F_STK & "', '" & STATUS & "')")
                'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
                'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
                MsgBox("ท่านได้ทำการเพิ่มข้อมูลหน่วยนับเรียบร้อยแล้ว")
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
            End Try
            connect.Dispose()
            'connectlocal.Dispose()
        End If
    End Sub

    Public Sub editMasunitData()
        Try
            connect = CONDB.NewConnection()
            'connectlocal = conlocal.NewConnection()

            connect.ExecuteNonQuery("UPDATE masunit SET unitname_en = '" & UNITNAME_EN & "', unitname_th = '" & UNITNAME_TH & "' , f_rx = '" & F_RX & "', f_lab = '" & F_LAB & "', f_xry = '" & F_XRY & "', f_time = '" & F_TIME & "', f_stk = '" & F_STK & "', status = '" & STATUS & "' WHERE unitid = '" & UNITID & "'")
            'connectlocal.ExecuteNonQuery("UPDATE MASDISCHARGE SET DISCHARGE = '" & masdischargeDescription & "', STATUS = '" & masdischargeStatus & "' WHERE DISCHGID = '" & masdischargeID & "'")
            MsgBox("ท่านได้ทำการแก้ไขข้อมูลหน่วยนับเรียบร้อยแล้ว")
        Catch ex As Exception
            MsgBox(vbCrLf & ex.Message)
        End Try
        connect.Dispose()
        'connectlocal.Dispose()
    End Sub
End Class
